package cn.u313.listener;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;

import cn.u313.comm.conststring.ImageConfig;
import cn.u313.comm.utils.MsgUtil;
import cn.u313.comm.utils.Util;
import cn.u313.config.BotConfig;
import cn.u313.test.Test1;
import com.alibaba.fastjson.JSON;
import lombok.val;
import love.forte.simbot.annotation.Filter;
import love.forte.simbot.annotation.OnGroup;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.sender.MsgSender;
import love.forte.simbot.filter.MatchType;
import love.forte.simbot.utils.MD5;
import me.kuku.utils.ImageHandle;
import me.kuku.utils.MyUtils;
import me.kuku.utils.OkHttpUtils;
import me.kuku.utils.gif.AnimatedGifEncoder;
import me.kuku.utils.gif.GifDecoder;
import org.springframework.stereotype.Component;


@Component
public class ImageListen extends BaseListen{
    MsgUtil mif = this;
    @OnGroup
    @Filter(value = "丢",matchType = MatchType.STARTS_WITH)
    public void diuAt(GroupMsg groupMsg, MsgSender msgSender){
        String qq =getQQ2(groupMsg);
        if (HasImageOut("diu_",qq)){
            String msg = getDiu(acatarUrl(qq),qq);
            sendMsg(msgSender,groupMsg,msg);
            return;
        }
        sendMsg(msgSender,groupMsg,getImageMsg("diu_",qq));

    }


    @OnGroup
    @Filter(value = "爬",matchType = MatchType.STARTS_WITH)
    public void paAt(GroupMsg groupMsg, MsgSender msgSender){

        String qq =getQQ2(groupMsg);
        if (HasImageOut("pa_",qq)){
            String url = acatarUrl(qq);
            String msg = "";
            if (MyUtils.randomInt(0, 100) > 50)
                msg = getPa(url,qq);
            else  msg = getSuperPa(url,qq);
            sendMsg(msgSender,groupMsg,msg);
            return;
        }
        sendMsg(msgSender,groupMsg,getImageMsg("pa_",qq));
    }


    @OnGroup
    @Filter(value = "嚼",matchType = MatchType.STARTS_WITH)
    @Filter(value = "恰",matchType = MatchType.STARTS_WITH)
    public void jiAo(GroupMsg groupMsg, MsgSender msgSender){
        String qq= getQQ2(groupMsg);
        if (HasImageOut("jiao_",qq)){

            String url = acatarUrl(qq);
            String msg =  getJiao(url,qq);
            sendMsg(msgSender,groupMsg,msg);
            return;
        }
        sendMsg(msgSender,groupMsg,getImageMsg("jiao_",qq));
    }

    @OnGroup
    @Filter(value = "mua",matchType = MatchType.STARTS_WITH)
    public void mua(GroupMsg groupMsg, MsgSender msgSender){
        String qq= getQQ2(groupMsg);
        if (HasImageOut("mua_",qq)){

            String url = acatarUrl(getQQ2(groupMsg));
            String msg = getMuaGif(url,qq);
            sendMsg(msgSender,groupMsg,msg);
            return;
        }
        sendMsg(msgSender,groupMsg,getImageMsg("mua_",qq));
    }

    @OnGroup
//    @Filter(value = "摸",matchType = MatchType.STARTS_WITH)
    @Filter(value = "rua",matchType = MatchType.STARTS_WITH)
    public void mo(GroupMsg groupMsg, MsgSender msgSender){
        String qq= getQQ2(groupMsg);
        if (HasImageOut("rua_",qq)){


            String url = acatarUrl(qq);
            String msg = getRua(url,qq);
            sendMsg(msgSender,groupMsg,msg);
            return;
        }
        sendMsg(msgSender,groupMsg,getImageMsg("rua_",qq));
    }
    @OnGroup
//    @Filter(value = "摸",matchType = MatchType.STARTS_WITH)
    @Filter(value = "顶",matchType = MatchType.STARTS_WITH)
    public void dingRex(GroupMsg groupMsg, MsgSender msgSender){
        String qq = getQQ2(groupMsg);
        if (HasImageOut("ding_",qq)){

            String url = acatarUrl(qq);
            String msg = ding(url,qq);
            sendMsg(msgSender,groupMsg,msg);
            return;
        }
        sendMsg(msgSender,groupMsg,getImageMsg("ding_",qq));
    }
    @OnGroup
//    @Filter(value = "摸",matchType = MatchType.STARTS_WITH)
    @Filter(value = "蹭",matchType = MatchType.STARTS_WITH)
    public void cengrex(GroupMsg groupMsg, MsgSender msgSender){
        String qq = getQQ2(groupMsg);

        if (HasImageOut("ceng_",qq)){
            String url = acatarUrl(qq);
            String msg = ceng(url,qq);
            sendMsg(msgSender,groupMsg,msg);
            return;
        }
        sendMsg(msgSender,groupMsg,getImageMsg("ceng_",qq));
    }


    private String acatarUrl(String qq){
        return "https://q2.qlogo.cn/g?b=qq&nk=" + qq + "&s=640";
    }

    private String getDiu(String url,String qq){
        BufferedImage headImage;
        BufferedImage bgImage;

        int hdW = 146;
        try {
            headImage = (BufferedImage) ImageHandle.getHeadImage(url, hdW);
            bgImage = ImageIO.read(getClass().getClassLoader().getResource("image/diu.png"));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        //处理头像
        BufferedImage formatAvatarImage = new BufferedImage(hdW, hdW, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D graphics = formatAvatarImage.createGraphics();
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿
        //图片是一个圆型
        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, hdW, hdW);
        //需要保留的区域
        graphics.setClip(shape);
        graphics.rotate(Math.toRadians(-50),hdW / 2,hdW / 2);
        graphics.drawImage(headImage.getScaledInstance(hdW,hdW,Image.SCALE_SMOOTH), 0, 0, hdW, hdW, null);
        graphics.dispose();

        //重合图片
        Graphics2D graphics2D = bgImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿
        graphics2D.drawImage(formatAvatarImage,110 - hdW / 2,275 - hdW / 2,hdW,hdW,null);//头画背景上
        graphics2D.dispose();

        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageIO.write(bgImage, "PNG", bos);
            return mif.imageByByteArray(bos.toByteArray(),qq,"diu_");
        } catch (IOException e) {
            return mif.text("图片发送失败：" + e.getMessage());
        }
    }

    private String getPa(String url,String qq){
        BufferedImage headImage;
        BufferedImage bgImage;
        int hdW = 65;

        try {
            headImage = (BufferedImage) ImageHandle.getHeadImage(url, hdW);
            bgImage = ImageIO.read(getClass().getClassLoader().getResource("image/pa/" + MyUtils.randomInt(1,16) + ".jpg"));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        //处理头像
        BufferedImage formatAvatarImage = new BufferedImage(hdW, hdW, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D graphics = formatAvatarImage.createGraphics();
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿
        //图片是一个圆型
        Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, hdW, hdW);
        //需要保留的区域
        graphics.setClip(shape);
        graphics.drawImage(headImage.getScaledInstance(hdW,hdW,Image.SCALE_SMOOTH), 0, 0, hdW, hdW, null);
        graphics.dispose();

        //重合图片
        Graphics2D graphics2D = bgImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿
        graphics2D.drawImage(formatAvatarImage,0,bgImage.getHeight() - hdW,hdW,hdW,null);//头画背景上
        graphics2D.dispose();

        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageIO.write(bgImage, "PNG", bos);
            return mif.imageByByteArray(bos.toByteArray(),qq,"pa_");
        } catch (IOException e) {
            e.printStackTrace();
            return mif.text("图片发送失败：" + e.getMessage());
        }
    }

    private String getJiao(String url,String qq){
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        BufferedImage jiao_top = null;
        try {
            jiao_top = ImageIO.read(getClass().getClassLoader().getResource("image/jiao/jiao_top"));
        } catch (IOException e) {
            e.printStackTrace();
            return mif.text("在生成图片时出现了一点点错误");
        }

        GifDecoder decoder = new GifDecoder();
        int status = 0;
        //w:60 h:67
        status = decoder.read(getClass().getResourceAsStream("/image/jiao/jiao.gif"));
        if (status != GifDecoder.STATUS_OK) {
            return mif.text("在生成图片时出现了一点点错误:" + status);
        }
        AnimatedGifEncoder animatedGifEncoder = new AnimatedGifEncoder();
        //保存的目标图片
        animatedGifEncoder.start(bos);
        animatedGifEncoder.setRepeat(decoder.getLoopCount());
        animatedGifEncoder.setDelay(decoder.getDelay(0));
        for (int i = 0; i < decoder.getFrameCount(); i++) {
            BufferedImage image = decoder.getFrame(i);
            //加入头像  直径：38
            int f = 38;
            //得到头
            BufferedImage headImage = (BufferedImage) ImageHandle.getHeadImage(url, f);
            //编辑头，加缺口
            Graphics2D headGraphics2D = headImage.createGraphics();
            headGraphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿
            headGraphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
            headGraphics2D.fillOval(21, 0, 8, 10);//画椭圆
            headGraphics2D.dispose();
            //把头放上去
            Graphics2D graphics = image.createGraphics();
            graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿
            graphics.drawImage(headImage,0,image.getHeight() - headImage.getHeight() + 6,f,f,null);//头画背景上
            //手
            graphics.drawImage(jiao_top,0,0,image.getWidth(),image.getHeight(),null);//把手画上
            graphics.dispose();

            animatedGifEncoder.addFrame(image);
        }
        if(animatedGifEncoder.finish()){
            return mif.imageByByteArray(bos.toByteArray(),qq,"jiao_");
        }

        return mif.text("在生成图片时出现了一点点错误");
    }

    private String getRua(String url,String qq){
        int hw = 80;//头像宽度
        //得到头像
        BufferedImage headImage = (BufferedImage) ImageHandle.getHeadImage(url, hw);
        //得到mo的gif
        GifDecoder decoder = new GifDecoder();
        int status = 0;
        //w:60 h:67
        status = decoder.read(getClass().getResourceAsStream("/image/mo/mo.gif"));
        if (status != GifDecoder.STATUS_OK) {
            return mif.text("在生成图片时出现了一点点错误:" + status);
        }
        AnimatedGifEncoder animatedGifEncoder = new AnimatedGifEncoder();
        //保存的目标图片
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        animatedGifEncoder.start(bos);
        animatedGifEncoder.setRepeat(decoder.getLoopCount());
        animatedGifEncoder.setDelay(decoder.getDelay(0));

        for (int i = 0; i < decoder.getFrameCount(); i++) {
            BufferedImage image = decoder.getFrame(i);
            //清除背景
            Graphics2D graphics2D = image.createGraphics();
            graphics2D.setColor(Color.getColor("#fffbf3"));
            graphics2D.fillRect(0,0,image.getWidth(),image.getHeight());
            //开始生成
            graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿
            try {
                int mfd = 15;//最大幅度
                int fd = 0;//幅度
                //得到手
                BufferedImage shou = ImageIO.read(getClass().getClassLoader().getResource("image/mo/" + (i + 1) + ".png"));
                int y = (image.getHeight() - hw - 10);
                //拉伸头
                switch (i + 1){
                    case 1:
                        fd = 0;
                        graphics2D.drawImage(headImage,20 - (fd / 2), y + fd,hw + fd, hw - fd,null);
                        break;
                    case 2:
                        fd = mfd / 2;
                        graphics2D.drawImage(headImage,20 - (fd / 2), y + fd,hw + fd, hw - fd,null);
                        break;
                    case 3:
                        fd = mfd;
                        graphics2D.drawImage(headImage,20 - (fd / 2),y + fd,hw + fd, hw - fd,null);
                        break;
                    case 4:
                        fd = mfd / 2 + 1;
                        graphics2D.drawImage(headImage,20 - (fd / 2),y + fd,hw + fd, hw - fd,null);
                        break;
                    case 5:
                        fd = 1;
                        graphics2D.drawImage(headImage,20,y,hw,hw,null);
                        break;
                }
                //放手
                graphics2D.drawImage(shou,0,0,shou.getWidth(),shou.getHeight(),null);
            } catch (IOException e) {
                e.printStackTrace();
                return mif.text("在生成图片时出现了一点点错误");
            }
            graphics2D.dispose();

            animatedGifEncoder.addFrame(image);
        }
        if(animatedGifEncoder.finish()){
            return mif.imageByByteArray(bos.toByteArray(),qq,"rua_");
        }
        return mif.text("在生成图片时出现了一点点错误");
    }

    private String getSuperPa(String url,String qq){
        int w = 360;
        int h = 360;
        int delay = 50;
        int hdW = 65;
        int paIndex = 16;

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        BufferedImage headImage = (BufferedImage) ImageHandle.getHeadImage(url, hdW);;

        AnimatedGifEncoder animatedGifEncoder = new AnimatedGifEncoder();
        animatedGifEncoder.setSize(w,h);
        animatedGifEncoder.start(bos);
        animatedGifEncoder.setDelay(delay);
        animatedGifEncoder.setRepeat(paIndex);

        for (int i = 0; i < paIndex; i++){
            BufferedImage bgImage;
            try {
                bgImage = ImageIO.read(getClass().getClassLoader().getResource("image/pa/" + (i + 1) + ".jpg"));
                BufferedImage bufferedImage = new BufferedImage(w,h,BufferedImage.TYPE_4BYTE_ABGR);
                Graphics2D graphics2D = bufferedImage.createGraphics();
                graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿
                graphics2D.drawImage(bgImage,0,0,w,h,null);
                graphics2D.drawImage(headImage,0,h - hdW,hdW,hdW,null);//头画背景上
                graphics2D.dispose();
                animatedGifEncoder.addFrame(bufferedImage);
            } catch (IOException e) {
                e.printStackTrace();
                return mif.text("在生成图片时出现了一点点错误");
            }
        }

        if(animatedGifEncoder.finish()){
            return mif.imageByByteArray(bos.toByteArray(),qq,"superpa_");
        }
        return mif.text("在生成图片时出现了一点点错误");
    }

    private String qing(String url,String qq){
        int bgWH = 240;//背景宽高
        int delay = 50;//每张图之间的延迟
        BufferedImage headImage = (BufferedImage) ImageHandle.getMemberHeadImageNoFrame(url,80);//得到头像

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        AnimatedGifEncoder animatedGifEncoder = new AnimatedGifEncoder();
        animatedGifEncoder.setSize(bgWH,bgWH);
        animatedGifEncoder.start(bos);
        animatedGifEncoder.setDelay(delay);
        animatedGifEncoder.setRepeat(13);

        //x,y,w,h
        int imageHeadInfo[][] = {
                {46,117,62,64},//1
                {68,107,63,66},//2
                {76,107,58,69},//3
                {55,123,58,63},//4
                {66,123,56,68},//5
                {71,122,54,66},//6
                {24,146,57,56},//7
                {32,128,71,72},//8
                {73,110,55,72},//9
                {57,118,54,65},//10
                {76,114,60,69},//11
                {47,137,56,66},//12
                {22,149,68,65}//13
        };//头像位置信息

        for (int i = 0; i < 13; i++){
            BufferedImage bgImage;
            try {
                //得到背景
                bgImage = ImageIO.read(getClass().getClassLoader().getResource("image/mua/" + (i + 1) + ".png"));
                //空白底
                BufferedImage bufferedImage = new BufferedImage(bgWH,bgWH,BufferedImage.TYPE_4BYTE_ABGR);
                Graphics2D graphics2D = bufferedImage.createGraphics();
                graphics2D.setColor(Color.WHITE);
                graphics2D.fillRect(0,0,bgWH,bgWH);
                graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿
                //先画头
                graphics2D.drawImage(headImage,imageHeadInfo[i][0],imageHeadInfo[i][1],imageHeadInfo[i][2],imageHeadInfo[i][3],null);
                //画背景
                graphics2D.drawImage(bgImage,0,0,bgWH,bgWH,null);
                graphics2D.dispose();
                animatedGifEncoder.addFrame(bufferedImage);
            } catch (IOException e) {
                e.printStackTrace();
                return mif.text("在生成图片时出现了一点点错误");
            }
        }

        if(animatedGifEncoder.finish()){
            return mif.imageByByteArray(bos.toByteArray(),qq,"qing_");
        }
        return mif.text("在生成图片时出现了一点点错误");
    }
    private final static String pathBase = BotConfig.getJarPath() + "/bot_220624/";
    private int repeat = 10000;


    private String ding(String url,String qq){
        val bgW = 480;
        val bgH = 400;
        val delay = 90 ;//每张图之间的延迟
        val headImage = ImageHandle.getMemberHeadImageNoFrame(url, 80) ;//得到头像
        val bos = new ByteArrayOutputStream();
        AnimatedGifEncoder animatedGifEncoder = new AnimatedGifEncoder();
        animatedGifEncoder.setSize(bgW, bgH);
        animatedGifEncoder.setDelay(delay);
        animatedGifEncoder.setRepeat(repeat);
        animatedGifEncoder.start(bos);
        val alpha = 0.6f; //透明度
        //x,y,w,h,color
        //头像位置信息
        int[][] imageHeadInfo = ImageConfig.getDingArr();
        for (int i = 0;i<imageHeadInfo.length;i++) {
            try {
                //InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config.json");
                BufferedImage bgImage  = ImageIO.read(getClass().getClassLoader()
                        .getResource("image/ding/"+(i+1)+".png"));
//                String path = (pathBase+"src\\main\\resources\\image\\ding\\" + (i + 1) + ".png");
                //得到背景
                //空白底
                val bufferedImage = new BufferedImage(bgW, bgH, BufferedImage.TYPE_4BYTE_ABGR);
                val graphics2D = bufferedImage.createGraphics();
                graphics2D.setColor(Color.WHITE);
                graphics2D.fillRect(0, 0, bgW, bgH);
                graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //抗锯齿
                //先画头
                if (imageHeadInfo[i][0] > 0) {
                    graphics2D.drawImage(
                            headImage,
                            imageHeadInfo[i][0],
                            imageHeadInfo[i][1],
                            imageHeadInfo[i][2],
                            imageHeadInfo[i][3],
                            null
                    );
                    if (imageHeadInfo[i][4] != -1) {
                        //颜色蒙版
                        graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
                        graphics2D.setColor(new Color(imageHeadInfo[i][4]));  //设置蒙版颜色
                        graphics2D.fillRect(0, 0, bgW, bgH);
                        graphics2D.setColor(Color.WHITE);
                        graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1f));
                    }
                }
                //画背景
                graphics2D.drawImage(bgImage, 0, 0, bgW, bgH, null);
                graphics2D.dispose();
                animatedGifEncoder.addFrame(bufferedImage);

            } catch (Exception e){
                e.printStackTrace();
            }
        }

        return imageByByteArray(bos.toByteArray(),qq,"ding_");
    }


    private String ceng(String url,String qq) {
        val bgW = 240;
        val bgH = 240;
        val delay = 50;//每张图之间的延迟
        val headImage = ImageHandle.getMemberHeadImageNoFrame(url, 80);
        val bos = new ByteArrayOutputStream();
        val animatedGifEncoder = new AnimatedGifEncoder();
        animatedGifEncoder.setSize(bgW, bgH);
        animatedGifEncoder.setDelay(delay);
        animatedGifEncoder.setRepeat(repeat);
        animatedGifEncoder.start(bos);

        //x,y,w,h
        int[][] imageHeadInfo = ImageConfig.getCengArr();
        //头像位置信息
        for (int i = 0; i < imageHeadInfo.length; i++) {
            try {
                //得到背景
                BufferedImage bgImage = ImageIO.read(getClass().getClassLoader()
                        .getResource("image/ceng/"+(i+1)+".png"));
                //ImageIO.read(new File(pathBase + "src\\main\\resources\\image/ceng/" + (i + 1) + ".png"));
                //空白底
                val bufferedImage = new BufferedImage(bgW, bgH, BufferedImage.TYPE_4BYTE_ABGR);
                val graphics2D = bufferedImage.createGraphics();
                graphics2D.setColor(Color.WHITE);
                graphics2D.fillRect(0, 0, bgW, bgH);
                graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //抗锯齿
                //先画头
                graphics2D.drawImage(
                        headImage,
                        imageHeadInfo[i][0], imageHeadInfo[i][1], imageHeadInfo[i][2], imageHeadInfo[i][3], null
                );
                //画背景
                graphics2D.drawImage(bgImage, 0, 0, bgW, bgH, null);
                graphics2D.dispose();
                animatedGifEncoder.addFrame(bufferedImage);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        return imageByByteArray(bos.toByteArray(),qq,"ceng_");
    }


    private String getMuaGif(String url,String qq){
        int bgWH = 240;//背景宽高
        int delay = 50;//每张图之间的延迟
        BufferedImage headImage = (BufferedImage) ImageHandle.getMemberHeadImageNoFrame(url,80);//得到头像

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        AnimatedGifEncoder animatedGifEncoder = new AnimatedGifEncoder();
        animatedGifEncoder.setSize(bgWH,bgWH);
        animatedGifEncoder.start(bos);
        animatedGifEncoder.setDelay(delay);
        animatedGifEncoder.setRepeat(13);

        //x,y,w,h
        int imageHeadInfo[][] = {
                {46,117,62,64},//1
                {68,107,63,66},//2
                {76,107,58,69},//3
                {55,123,58,63},//4
                {66,123,56,68},//5
                {71,122,54,66},//6
                {24,146,57,56},//7
                {32,128,71,72},//8
                {73,110,55,72},//9
                {57,118,54,65},//10
                {76,114,60,69},//11
                {47,137,56,66},//12
                {22,149,68,65}//13
        };//头像位置信息

        for (int i = 0; i < 13; i++){
            BufferedImage bgImage;
            try {
                //得到背景
                bgImage = ImageIO.read(getClass().getClassLoader().getResource("image/mua/" + (i + 1) + ".png"));
                //空白底
                BufferedImage bufferedImage = new BufferedImage(bgWH,bgWH,BufferedImage.TYPE_4BYTE_ABGR);
                Graphics2D graphics2D = bufferedImage.createGraphics();
                graphics2D.setColor(Color.WHITE);
                graphics2D.fillRect(0,0,bgWH,bgWH);
                graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿
                //先画头
                graphics2D.drawImage(headImage,imageHeadInfo[i][0],imageHeadInfo[i][1],imageHeadInfo[i][2],imageHeadInfo[i][3],null);
                //画背景
                graphics2D.drawImage(bgImage,0,0,bgWH,bgWH,null);
                graphics2D.dispose();
                animatedGifEncoder.addFrame(bufferedImage);
            } catch (IOException e) {
                e.printStackTrace();
                return mif.text("在生成图片时出现了一点点错误");
            }
        }

        if(animatedGifEncoder.finish()){
            return mif.imageByByteArray(bos.toByteArray(),qq,"mua_");
        }
        return mif.text("在生成图片时出现了一点点错误");
    }

}
